clear
set more      off
set varabbrev off
set gr        off
set linesize 255

/* INPUTS */

local in_anprep "/data2/cedsei/data2/analysis_prep" // Produced in A

/* OUTPUTS */


*cd "/data2/cedsei/data2"

local binsize= 10

********************************************************************************

clear 
use `in_anprep'

foreach num of numlist 2(1)6 {
	local kk = `num'-1
	replace workabs_`num'=. if workabs_`kk'==1 | workabs_`kk'==.
}

foreach num of numlist 1(1)6 {
	gen dwage_`num'=cond(!mi(workabs_`num'),cond(workabs_`num'==1,log(manl_`num')-log(manl_l1),.),.)	
}

replace search_int = ln(search_int + sqrt(search_int^2 +1))
tempfile a0 
save 	`a0', replace 


replace runvar = runvar /1000 
replace T = T / 1000


	
*eststo: reg replacement_rate 	runvar T jump,	cluster(lopnr)
*Reduced form estimates 
eststo clear
eststo: reg workabs_1 		runvar T jump,	cluster(lopnr) 
esttab using "$agetablepath/Search/col2_row1.tex",  notype b(4) se(4) keep(T)  replace wide ///
		nonotes noobs nolab nomtitle nodepvar nonumber prehead("") posthead("") prefoot("")  postfoot("") end("") begin("") ///
		varlab(none)  collabels(none) star(* 0.1 ** 0.05 *** 0.01)

eststo clear
eststo: reg dwage_1 		runvar T jump,	cluster(lopnr) 
esttab using "$agetablepath/Search/col2_row2.tex",  notype b(4) se(4) keep(T)  replace wide ///
		nonotes noobs nolab nomtitle nodepvar nonumber prehead("") posthead("") prefoot("")  postfoot("") end("") begin("") ///
		varlab(none)  collabels(none) star(* 0.1 ** 0.05 *** 0.01)

eststo clear		
eststo: reg search_ext		runvar T jump,	cluster(lopnr) 
esttab using "$agetablepath/Search/col2_row3.tex",  notype b(4) se(4) keep(T)  replace wide ///
		nonotes noobs nolab nomtitle nodepvar nonumber prehead("") posthead("") prefoot("")  postfoot("") end("") begin("") ///
		varlab(none)  collabels(none) star(* 0.1 ** 0.05 *** 0.01)

eststo clear		
eststo: reg search_ind		runvar T jump,	cluster(lopnr) 
esttab using "$agetablepath/Search/col2_row4.tex",  notype b(4) se(4) keep(T)  replace wide ///
		nonotes noobs nolab nomtitle nodepvar nonumber prehead("") posthead("") prefoot("")  postfoot("") end("") begin("") ///
		varlab(none)  collabels(none) star(* 0.1 ** 0.05 *** 0.01)

eststo clear		
eststo: reg search_int		runvar T jump,	cluster(lopnr) 
esttab using "$agetablepath/Search/col2_row5.tex",  notype b(4) se(4) keep(T)  replace wide ///
		nonotes noobs nolab nomtitle nodepvar nonumber prehead("") posthead("") prefoot("")  postfoot("") end("") begin("") ///
		varlab(none)  collabels(none) star(* 0.1 ** 0.05 *** 0.01)


matrix B = J(3,3,.)		
*IV estimates
eststo clear	
reg search_ext		runvar T jump,	cluster(lopnr) 
	predict search_hat,xb
	scalar s1=e(rmse)^2
eststo: reg workabs_1		search_hat , cluster(lopnr) 
	matrix B[1,1] = _b[search_hat]
	matrix V = e(V)
	scalar correct=sqrt(1+_b[search_hat]^2*s1/(e(rmse)^2))
	di "SE of coefficient on search1 = " correct*sqrt(V[1,1]) // plug in manually in table
	matrix B[1,2] = correct*sqrt(V[1,1])
drop search_hat
esttab using "$agetablepath/Search/col2_row6.tex",  notype b(4) se(4) keep(search_hat)  replace wide ///
		nonotes noobs nolab nomtitle nodepvar nonumber prehead("") posthead("") prefoot("")  postfoot("") end("") begin("") ///
		varlab(none)  collabels(none) star(* 0.1 ** 0.05 *** 0.01)

eststo clear	
reg search_ind		runvar T jump,	cluster(lopnr) 
	predict search_hat,xb
	scalar s1=e(rmse)^2
eststo: reg workabs_1		search_hat , cluster(lopnr) 
	matrix B[2,1] = _b[search_hat]
	matrix V = e(V)
	scalar correct=sqrt(1+_b[search_hat]^2*s1/(e(rmse)^2))
	di "SE of coefficient on search1 = " correct*sqrt(V[1,1]) // plug in manually in table
	matrix B[2,2] = correct*sqrt(V[1,1])
drop search_hat
esttab using "$agetablepath/Search/col2_row7.tex",  notype b(4) se(4) keep(search_hat)  replace wide ///
		nonotes noobs nolab nomtitle nodepvar nonumber prehead("") posthead("") prefoot("")  postfoot("") end("") begin("") ///
		varlab(none)  collabels(none) star(* 0.1 ** 0.05 *** 0.01)

eststo clear
reg search_int		runvar T jump,	cluster(lopnr) 
	predict search_hat,xb
	scalar s1=e(rmse)^2
eststo: reg workabs_1		search_hat , cluster(lopnr) 
	matrix B[3,1] = _b[search_hat]
	matrix V = e(V)
	scalar correct=sqrt(1+_b[search_hat]^2*s1/(e(rmse)^2))
	di "SE of coefficient on search1 = " correct*sqrt(V[1,1]) // plug in manually in table
	matrix B[3,2] = correct*sqrt(V[1,1])
drop search_hat
esttab using "$agetablepath/Search/col2_row8.tex",  notype b(4) se(4) keep(search_hat)  replace wide ///
		nonotes noobs nolab nomtitle nodepvar nonumber prehead("") posthead("") prefoot("")  postfoot("") end("") begin("") ///
		varlab(none)  collabels(none) star(* 0.1 ** 0.05 *** 0.01)

preserve			
	matrix list B		
	svmat B
	keep in 1/3
	keep B1-B3
	tostring B3,replace
	replace B3 = "Extensive search" in 1
	replace B3 = "Outside PES search" in 2
	replace B3 = "Search intensity" in 3
	rename B1 b
	rename B2 se
	rename B3 meassure
	gen analysis = "RKD"

	save "$datapath/Searchint_meassures_RKD.dta",replace
restore

clear 
use `a0' 

qui foreach var of varlist workabs_* dwage* replacement_rate search_ext search_ind search_int {
	reg `var' runvar T jump,	cluster(lopnr)
	local b`var' = round(_b[T]*1000,.0001)
	local s`var' = round(_se[T]*1000,.0001)
	local t`var'_`co' = `b`var''/`s`var''
}

local binsize= 10
replace daily_wage_normalized=round(runvar/`binsize')*`binsize'

gcollapse (mean) runvar workabs_* replacement_rate dwage_*, by(daily_wage_normalized)

local co =1 
foreach var of varlist workabs_1 dwage_1 replacement_rate {
	if inlist("`var'","workabs_1") {
		local yy = "Hazard to new job"
		local yloc = 0.057
		local xloc = -175
		local y1 = 0.05
		local ym = 0.005
		local y2 = 0.065

	}
	else if inlist("`var'","replacement_rate") {
		local yy = "Replacement rate"
		local yloc = 0.68
		local xloc = -175
		local y1 = 0.6
		local ym = 0.05
		local y2 = 0.8


	}
	else if inlist("`var'","dwage_1") {
		local yy = "Change in log-wages"
		local y1 = 0.005
		local ym = 0.005
		local y2 = 0.045
		local yloc = 0.02
		local xloc = -175

	}
	if `b`var''<0 {
		local ss = "-0"
	}
	else if `b`var''>0 {
		local ss = "0"
	}
	local absb = abs(`b`var'')
	
	
	twoway (scatter `var'  runvar , ms(oh) lc(navy) mc(navy)) ///
		(lfit `var'  runvar if daily_wage_normalized<=0, lcolor(maroon)) ///
		(lfit `var'  runvar if daily_wage_normalized>=0, lcolor(maroon)) ///
	, xline(0, lcolor(black) lstyle(foreground) lpattern(dash)) ///
	graphregion(color(white)) legend(off) ///
	xtitle("Daily Wage Relative to Threshold (SEK)") ytitle("`yy'") ///
	xscale(r(-300 300)) xlab(-300(50)300)  ///
	text(`yloc' `xloc' "Estimated change in slopes" "=`ss'`absb' (0`s`var'')" ) 
	
	if `co'<10 {
		graphexportpdf atemp_a_0`co', replace		
	}
	else {
		graphexportpdf atemp_a_`co', replace		
	}
	local co = `co'+ 1
}

clear 
use `a0'
local binsize= 15
replace daily_wage_normalized=round(runvar/`binsize')*`binsize'

gcollapse (mean) runvar search_ind search_ext search_int, by(daily_wage_normalized)

foreach var of varlist  search_ind search_ext search_int {
	if inlist("`var'","search_ind") {
		local yy = "P(Searching outside PES)"
		local y1 = 0.45 
		local ym = 0.025
		local y2 = 0.65	
		local th = 0.56
		local ss = "0"
	}
	else if inlist("`var'","search_ext") {
		local yy = "P(Searching)"
		local y1 = 0.55 
		local ym = 0.05
		local y2 = 0.75		
		local th = 0.69 
		local ss = "0"
	}
	else if inlist("`var'","search_int") {
		local yy = "P(Searching, scale)"
		local y1 = 1.5 
		local ym = 0.25
		local y2 = 2.5		
		local th = 2.35 
		local ss = ""
	}
	twoway (scatter `var'  runvar , ms(oh) lc(navy) mc(navy)) ///
		(lfit `var'  runvar if daily_wage_normalized<=0, lcolor(maroon)) ///
		(lfit `var'  runvar if daily_wage_normalized>=0, lcolor(maroon)) ///
	, xline(0, lcolor(black) lstyle(foreground) lpattern(dash)) ///
	graphregion(color(white)) legend(off) ///
	xtitle("Daily Wage Relative to Threshold (SEK)") ytitle("`yy'") ///
	xscale(r(-300 300)) ylab(`y1'(`ym')`y2') xlab(-300(50)300)  ///
	text(`th' -175 "Estimated change in slopes" "=`ss'`b`var'' (0`s`var'')" ) 
	if `co'<10 {
		graphexportpdf atemp_a_0`co', replace		
	}
	else {
		graphexportpdf atemp_a_`co', replace		
	}
	local co = `co'+ 1
}

!gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile=RKD_h1_"$S_DATE".pdf atemp*.pdf

!rm -f *temp*.pdf
!rm -f                 *.eps
